package com.cc.jdkjucdemo.CompletableFuture;

import java.util.concurrent.*;

/**
 * 顺序执行，串行化
 */
public class CompletableFutureDemo01_01 {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 50, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程start ......");
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("子线程执行了：" + Thread.currentThread().getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "a";
        }, executor);

        CompletableFuture<String> integerCompletableFuture = stringCompletableFuture.thenApply(result -> {
            System.out.println("thenApply执行了：" + Thread.currentThread().getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return result + "b";
        });
        String result = integerCompletableFuture.get();
        System.out.println("result = " + result);

        System.out.println("主线程end ......");
        executor.shutdown();
    }
}